<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/26
 * Time: 17:22
 */

namespace Home\Controller;
use Think\Controller;

class UsersController extends CommonController
{
    private $userID = '';
    private $username = '';
    private $Users = '';
    private $Region = '';
    private $userAddress = '';
    private $orderInfo='';
    private $orderGoods='';
    private $goods='';
    private $Seller='';

    function __construct()
    {
        parent::__construct();
        $this->userID = session("userID");
        $this->username = session("username");
        $this->Users = D('Users');
        $this->Region = D('Region');
        $this->userAddress = D('UserAddress');
        $this->orderInfo=D('Order_info');
        $this->orderGoods=D('Order_goods');
        $this->goods=D("goods");
        $this->Seller=D("Seller");
    }

    function index()
    {
        $userInfo = $this->Users->where("id=" . $this->userID)->find();
        $this->assign("userInfo", $userInfo);
        $this->assign("userID",$this->userID);
        $this->display();
    }

    function updateInfo()
    {
        if ($_POST) {
            $data['nick'] = $_POST['nick'];
            if (!empty($_POST['year']) & !empty($_POST['month']) & !empty($_POST['day'])) {
                $data['birthday'] = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
            }
            $data['sex'] = $_POST['sex'];
            $data['email'] = $_POST['email'];
            $data['tel']=$_POST['tel'];

            $aa = $this->Users->where("id=" . $this->userID)->save($data);
            if ($aa == true) {
                $this->success("数据修改成功");
            } else {
                $this->error("数据修改失败");
            }
        } else {
            $userInfo = $this->Users->where("id=" . $this->userID)->find();
            $yanGone = explode("-", $userInfo['birthday']);
            $userInfo['year'] = $yanGone[0];
            $userInfo['month'] = $yanGone[1];
            $userInfo['day'] = $yanGone[2];
            $this->assign("userInfo", $userInfo);
            $this->display("updateInfo");
        }
    }

    function delSession()
    {
        session("userID",null);
        session("openID",null);
        $this->redirect('Index/lbs');
    }

    function address()
    {
        $arr=$this->userAddress->where("user_id=".$this->userID)->select();

        foreach($arr as $key=>$ro)
        {
            if(!empty($ro['province']))
            {
                $t1=$this->Region->field(array('region_name'))->where("region_id=".$ro['province'])->find();
                $arr[$key]['provinceName']=$t1['region_name'];
            }else{
                $arr[$key]['provinceName']="无";
            }

            if(!empty($ro['city']))
            {
                $t2=$this->Region->field(array('region_name'))->where("region_id=".$ro['city'])->find();
                $arr[$key]['cityName']=$t2['region_name'];
            }else{
                $arr[$key]['cityName']="无";
            }

            if(!empty($ro['zone']))
            {
                $t3=$this->Region->field(array('region_name'))->where("region_id=".$ro['zone'])->find();
                $arr[$key]['zoneName']=$t3['region_name'];
            }else{
                $arr[$key]['zoneName']="无";
            }
        }
        $this->assign("addressList",$arr);
        $this->display();
    }

    function addAddress()
    {
        if($_POST)
        {
            $data=$_POST;
            $data['id']='';
            $data['user_id']=$this->userID;
            $data['consignee']=$_POST['name'];
            $aa=$this->userAddress->data($data)->add();
            if($aa==true)
            {
                $this->success("添加成功");
            }else{
                $this->error("添加失败");
            }
        }else{
            $province=$this->Region->where("region_type=1")->select();   // region_type=1 省
            $this->assign("province",$province);
            $this->display("addAddress");
        }
    }

    // 此处ID为地址信息的ID
    function updataAddress($id='')
    {

        if($_POST)
        {
            $id=$_POST['id'];
            $data['consignee']=$_POST['name'];
            $data['tel']=$_POST['tel'];

            if($data['province']!=''&&$data['city']!=''&&$data['zone']!='')
            {
                $data['province']=$_POST['province'];
                $data['city']=$_POST['city'];
                $data['zone']=$_POST['zone'];
            }
            $data['address']=$_POST['address'];
            $aa=$this->userAddress->where("id=".$id)->data($data)->save();
            if($aa==true)
            {
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }else{
            $addressInfo=$this->userAddress->where('id='.$id)->find();

//目前还不需要 省 和 市
//            $t1=$this->Region->field(array('region_id','region_name'))->where("region_id=".$addressInfo['province'])->find();
//            $addressInfo['provinceID']=$t1['region_id'];
//            $addressInfo['provinceName']=$t1['region_name'];
//
//            $t2=$this->Region->field(array('region_id','region_name'))->where("region_id=".$addressInfo['city'])->find();
//            $addressInfo['cityID']=$t2['region_id'];
//            $addressInfo['cityName']=$t2['region_name'];

            $t3=$this->Region->field(array('region_id','region_name'))->where("region_id=".$addressInfo['zone'])->find();
            $addressInfo['zoneID']=$t3['region_id'];
            $addressInfo['zoneName']=$t3['region_name'];

            $this->assign("addressInfo",$addressInfo);

            $province=$this->Region->where("region_type=1")->select();   // region_type=1 省
            $this->assign("province",$province);

            $this->display();
        }
    }

    function del($id='')
    {
        $aa=$this->userAddress->where("id=".$id)->delete();
        if($aa==true)
        {
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }


    //设置默认地址   此功能通过ajax 完成
    function signBuild()
    {
        $addressID=$_POST['id'];  // 获得 地址ID
        $data['sign_build']='0';
        $this->userAddress->where("user_id=".$this->userID)->data($data)->save();    // 设置默认地址钱，将此用户下的所有地址设置为0 ，为就是都不为默认地址
        $eata['sign_build']='1';
        $aa=$this->userAddress->where("id=".$addressID)->data($eata)->save();   // 将此ID的地址的sign_bulid 设置为1
        if($aa==true)
        {
            echo "true";
        }else{
            echo "false";
        }
    }

    //订单的详细信息 $id 是 订单的ID
    function myOrderInfo($id='')
    {
        $yanG1=$this->orderInfo->where("id=".$id)->find();   // 订单信息

        if($yanG1['order_status']=="0")
        {
            $yanG1['order_status_b']="未发货";
        }elseif($yanG1['order_status']=="1")
        {
            $yanG1['order_status_b']="(客户)交易完成";
        }elseif($yanG1['order_status']=="2")
        {
            $yanG1['order_status_b']="交易最终完成";
        }

        if($yanG1['pay_type']=="0")
        {
            $yanG1['pay_type_b']="货到付款";
        }elseif($yanG1['pay_type']=="1")
        {
            $yanG1['pay_type_b']="微信支付";
        }
        if($yanG1['shipping_type']=="1")   // 门店自提
        {
            $sellerInfo=$this->Seller->where("id=".$yanG1['seller_id'])->find();;
            $yanG1['shipping_type_b']="门店自提(".$sellerInfo['nick']."门店地址".$sellerInfo['address'].")";
        }else{
            $yanG1['shipping_type_b']="送货";
        }

        $yanG2=$this->orderGoods->field("goods_id,goods_number")->where("order_id=".$id)->select();
        foreach($yanG2 as $key=>$r)
        {
            $arr=$this->goods->field("name,shop_price,thumb,seller_id")->where("id=".$r['goods_id'])->find();
            $yanG2[$key]['name']=$arr['name'];
            $yanG2[$key]['shop_price']=$arr['shop_price'];
            if($arr['seller_id']!=0)
            {
                $yanG2[$key]['thumb']="seller/thumb/".$arr['thumb'];
            }else{
                $yanG2[$key]['thumb']="thumb/".$arr['thumb'];
            }

            $yanG2[$key]['singlePriceCount']=$r['goods_number']*$arr['shop_price'];
        }

        $this->assign("orderInfo",$yanG1);
        $this->assign("orderGoods",$yanG2);
        $this->assign("goodsAmount",$yanG1['goods_amount']);
        $this->assign("pay_status",$yanG1['pay_status']);
        $this->display();
    }

    //导出此用户的订单列表
    function myOrder()
    {
        $yanG1['user_id']=$this->userID;
        $yanG1['is_del']="0";
        $arr=$this->orderInfo->where($yanG1)->select();

        foreach($arr as $key=>$r)
        {
            $arr[$key]['timee']=date('Y-m-d H:i:s',$r['add_time']);
            if($r['order_status']==0)
            {
                $arr[$key]['order_status_b']="未发货";
            }
            elseif($r['order_status']==1)
            {
                $arr[$key]['order_status_b']="已发货";
            }
            elseif($r['order_status']==2)
            {
                $arr[$key]['order_status_b']="已收获";
            }
            elseif($r['order_status']==3)
            {
                $arr[$key]['order_status_b']="退货";
            }
        }

        foreach($arr as $key=>$ro)
        {
            if($ro['pay_status']==0)
            {
                $arr[$key]['pay_status_b']="未付款";
            }
            elseif($ro['pay_status']==2)
            {
                $arr[$key]['pay_status_b']="已付款";
            }
        }
        $this->assign("myOrder",$arr);
        $this->display();
    }

    //删除订单  $id 是 order id
    function delOrder($id='')
    {
        //第一步 在 order_goods 表中 ，删除 相应的 ￥id 的信息
        $yanG1['is_del']="1";
        $this->orderInfo->where("id=".$id)->save($yanG1);
        $this->success("订单删除成功",U('Users/myOrder'));
        exit;
    }

    // 三级联动  城市表
    function city()
    {
        $city=$this->Region->where("parent_id=".$_POST['id'])->select();
        $str='';
        foreach($city as $key=>$r)
        {
            $str.="<option value=".$r['region_id'].">".$r['region_name']."</option>";
        }
        echo $str;
        exit;
    }

    // 三级联动 地区表
    function zone()
    {
        $zone=$this->Region->where("parent_id=".$_POST['id'])->select();
        $str='';
        foreach($zone as $key=>$r)
        {
            $str.="<option value=".$r['region_id'].">".$r['region_name']."</option>";
        }
        echo $str;
        exit;
    }
}